package com.live.dengdengtv.activity.setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.live.dengdengtv.BaseActivity;
import com.live.dengdengtv.R;

public class EditInfoActivity extends BaseActivity {

    public final static String RETURN_EXTRA = "result";

    public static final int BG_RED = 1;
    public static final int BG_BLUE = -1;

    public static final int TYPE_AVATAR = 1;
    public static final int TYPE_TEXT = 2;
    public static final int TYPE_SEX = 3;
    public static final int TYPE_SIGN = 4;
    private static int type;
    private static int bg;
    private static String title;
    private EditText inputeSingle_et;
    private TextView title_right_tv;
    private String sex;
    private EditText inputeMulti_et;

    //    public static final TYPE_PHONE = ;
//    public static final TYPE_ = ;
//    public static final TYPE_ = ;
//    public static final TYPE_ = ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_info);

        LinearLayout background_ll = (LinearLayout) findViewById(R.id.background_ll);
        RelativeLayout title_rl = (RelativeLayout) findViewById(R.id.title_rl);

//        Intent intent = getIntent();
//        type = intent.getIntExtra("type", EditInfoActivity.BG_RED);
//        bg = intent.getIntExtra("background", 0);
//        title = intent.getStringExtra("title");


        setTheme(background_ll, title_rl, bg);
        initTitle(title,"保存");
        setDisplayView(background_ll, type, title);
    }

    private void setDisplayView(LinearLayout background_ll, int type, String title) {

        if(type == EditInfoActivity.TYPE_TEXT){

            setSimpleTextView(background_ll, title);
        }else if(type == EditInfoActivity.TYPE_SIGN){

            setMultiTextView(background_ll, title);
        }else if(type == EditInfoActivity.TYPE_SEX){

            setSexChooseView(background_ll, title);
        }else if(type == 0){
            throw new RuntimeException("Bundle未传值: type");
        }
    }

    private void setSimpleTextView(LinearLayout background_ll, String title) {
        LayoutInflater inflater = getLayoutInflater();
        inflater.inflate(R.layout.inpute_withdelete, background_ll, true);
        inputeSingle_et = (EditText)findViewById(R.id.inputeSingle_et);

        inputeSingle_et.setHint("请人输入您的"+ title);
        ImageButton delete_ib = (ImageButton)findViewById(R.id.delete_ib);
        delete_ib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputeSingle_et.setText("");
            }
        });

        title_right_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = inputeSingle_et.getText().toString().trim();
                if(TextUtils.isEmpty(result)){
                    return;
                }
                Intent intent  = new Intent();
                intent.putExtra(EditInfoActivity.RETURN_EXTRA, result);
                setResult(Activity.RESULT_OK,intent);
                finish();
            }
        });
    }
    private void setMultiTextView(LinearLayout background_ll, String title) {
        LayoutInflater inflater = getLayoutInflater();
        inflater.inflate(R.layout.inpute_multiline, background_ll, true);
        inputeMulti_et = (EditText)findViewById(R.id.inputeMulti_et);

        inputeMulti_et.setHint("请人输入您的"+ title);
        final TextView textNum_tv = (TextView)findViewById(R.id.textNum_tv);
        inputeMulti_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                textNum_tv.setText((30-s.length())+"");;
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        title_right_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = inputeMulti_et.getText().toString().trim();
                if(TextUtils.isEmpty(result)){
                    return;
                }
                Intent intent  = new Intent();
                intent.putExtra(EditInfoActivity.RETURN_EXTRA, result);
                setResult(Activity.RESULT_OK,intent);
                finish();
            }
        });
    }

    private void setSexChooseView(LinearLayout background_ll, String title) {
        sex = "";

        LayoutInflater inflater = getLayoutInflater();
        inflater.inflate(R.layout.edit_sex, background_ll, true);
        final ImageView  man_imageView = (ImageView)findViewById(R.id.man_imageView);
        final ImageView  womeman_imageView = (ImageView)findViewById(R.id.womeman_imageView);

        man_imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                man_imageView.setImageResource(R.mipmap.sex_man_press);
                womeman_imageView.setImageResource(R.mipmap.sex_women_normal);
                sex = "男";
            }
        });
        womeman_imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                man_imageView.setImageResource(R.mipmap.sex_man_normal);
                womeman_imageView.setImageResource(R.mipmap.sex_women_press);
                sex = "女";
            }
        });

        title_right_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent  = new Intent();
                intent.putExtra(EditInfoActivity.RETURN_EXTRA, sex);
                if(TextUtils.isEmpty(sex)){
                    setResult(Activity.RESULT_CANCELED,intent);
                }else{
                    setResult(Activity.RESULT_OK,intent);
                }

                finish();
            }
        });
    }

    private void initTitle(String title, String rightText) {
        ImageView title_left_iv = (ImageView) findViewById(R.id.title_left_iv);
        title_left_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(Activity.RESULT_CANCELED);
                finish();
            }
        });

        TextView title_center_tv = (TextView) findViewById(R.id.title_center_tv);
        title_center_tv.setText(title);

        title_right_tv = (TextView) findViewById(R.id.title_right_tv);
        title_right_tv.setText(rightText);
    }

    private void setTheme(LinearLayout background_ll, RelativeLayout title_rl, int bg) {
        if(bg == EditInfoActivity.BG_RED){
//            background_ll.setBackgroundResource(R.mipmap.bg_login);
            background_ll.setBackgroundResource(R.color.appLayout_bg_white);
            title_rl.setBackgroundResource(R.color.app_red_deep);
        }else {
            background_ll.setBackgroundResource(R.mipmap.bg_login);
            title_rl.setBackgroundResource(R.color.appLayout_bg_blue);
        }
    }

    public static void startEditInfoActivity(Activity activity, int type, int bg, String title, int reqCode){

        EditInfoActivity.type = type;
        EditInfoActivity.bg = bg;
        EditInfoActivity.title = title;
        Intent intent = new Intent(activity, EditInfoActivity.class);
        activity.startActivityForResult(intent, reqCode);
    }

//    public static void startEditInfoActivity(Activity activity, Bundle bundle){
//        Intent intent = new Intent(activity, EditInfoActivity.class);
//        intent.putExtras(bundle);
//        activity.startActivity(intent);
//    }

}
